<?php
/**
 * 用户持有资产
 */
namespace app\common\model;

use think\Model;
use app\common\model\Currency as CurrencyModel;

class MemberCurrency extends Model
{
    protected $name = 'member_currency';

    /**
     * 更新和获取 用户币种资产
     * @param $mid 用户ID
     * @return array 用户资产
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function member_renew($mid){  //更新用户币种列表
        $currency = CurrencyModel::getAll(['status'=>1,'business'=>1])->toArray();
        $currency_id_arr = [];
        foreach ($currency as $v){ $currency_id_arr[$v['id']] = $v; }

        //用户资产
        $currency = array();

        //查询用户当前资产
        $member_currency = self::where('mid',$mid)->select();

        $total = 0; //资产非冻结总量
        $total_freeze = 0; //资产冻结总量
        //资产数据为空
        if(empty($member_currency)){
            foreach ($currency as $k => $v){
                //更新用户资产
                $data = ['mid'=>$mid,'cid'=>$v['id'],'add_time'=>date('Y-m-d H:i:s',time())];
                self::insert($data);
                $currency[$v['id']] = $data;
                $currency[$v['id']]['name'] = $v['name'];  //币种名称
                $currency[$v['id']]['money'] = 0;  //持有金额
                $currency[$v['id']]['freeze_money'] = 0;  //冻结金额
                $currency[$v['id']]['name'] = $v['name'];  //币种名称
                $currency[$v['id']]['today_price'] = $v['today_price'];    //今日凌晨价格
                $currency[$v['id']]['current_price'] = $v['current_price'];    //当前价格

                $currency[$v['id']]['exchange'] = $v['exchange'];    //兑换功能 1:开启 2:关闭
                $currency[$v['id']]['ec_type'] = $v['ec_type'];    //兑换 0:不收取手续费 1:固定收取 2:百分
                $currency[$v['id']]['ec'] = $v['ec'];    //兑换手续费

                $currency[$v['id']]['transfer'] = $v['transfer'];    //划转功能 1:开启 2:关闭
                $currency[$v['id']]['tc_type'] = $v['tc_type'];    //划转 0:不收取手续费 1:固定收取 2:百分比收取
                $currency[$v['id']]['tc'] = $v['tc'];    //划转手续费
            }
        }else{
            //资产
            $member_currency = $member_currency->toArray();
            //统计资产
            foreach ($member_currency as $k => $v){
                if(!empty($currency_id_arr[$v['cid']])){
                    unset($v['id']);
                    $currency[$v['cid']] = $v;
                    $total = bcadd($total,bcmul($v['money'],$currency_id_arr[$v['cid']]['current_price'],10),10);
                    $total_freeze = bcadd($total_freeze,bcmul($v['freeze_money'],$currency_id_arr[$v['cid']]['current_price'],10),10);
                    $currency[$v['cid']]['name'] = $currency_id_arr[$v['cid']]['name'];  //币种名称
                    $currency[$v['cid']]['today_price'] = $currency_id_arr[$v['cid']]['today_price'];    //今日凌晨价格
                    $currency[$v['cid']]['current_price'] = $currency_id_arr[$v['cid']]['current_price'];    //当前价格

                    $currency[$v['cid']]['exchange'] = $currency_id_arr[$v['cid']]['exchange'];    //兑换功能 1:开启 2:关闭
                    $currency[$v['cid']]['ec_type'] = $currency_id_arr[$v['cid']]['ec_type'];    //兑换 0:不收取手续费 1:固定收取 2:百分
                    $currency[$v['cid']]['ec'] = $currency_id_arr[$v['cid']]['ec'];    //兑换手续费

                    $currency[$v['cid']]['transfer'] = $currency_id_arr[$v['cid']]['transfer'];    //划转功能 1:开启 2:关闭
                    $currency[$v['cid']]['tc_type'] = $currency_id_arr[$v['cid']]['tc_type'];    //划转 0:不收取手续费 1:固定收取 2:百分比收取
                    $currency[$v['cid']]['tc'] = $currency_id_arr[$v['cid']]['tc'];    //划转手续费

                    unset($currency_id_arr[$v['cid']]);

                }
            }

            //添加更新的资产
            if(!empty($currency_id_arr)){
                foreach ($currency_id_arr as $v){
                    //更新用户资产
                    $data = ['mid'=>$mid,'cid'=>$v['id'],'add_time'=>date('Y-m-d H:i:s',time())];
                    self::insert($data);
                    $currency[$v['id']] = $data;
                    $currency[$v['id']]['name'] = $v['name'];  //币种名称
                    $currency[$v['id']]['money'] = 0;  //持有金额
                    $currency[$v['id']]['freeze_money'] = 0;  //冻结金额
                    $currency[$v['id']]['name'] = $v['name'];  //币种名称
                    $currency[$v['id']]['today_price'] = $v['today_price'];    //今日凌晨价格
                    $currency[$v['id']]['current_price'] = $v['current_price'];    //当前价格

                    $currency[$v['id']]['exchange'] = $v['exchange'];    //兑换功能 1:开启 2:关闭
                    $currency[$v['id']]['ec_type'] = $v['ec_type'];    //兑换 0:不收取手续费 1:固定收取 2:百分
                    $currency[$v['id']]['ec'] = $v['ec'];    //兑换手续费

                    $currency[$v['id']]['transfer'] = $v['transfer'];    //划转功能 1:开启 2:关闭
                    $currency[$v['id']]['tc_type'] = $v['tc_type'];    //划转 0:不收取手续费 1:固定收取 2:百分比收取
                    $currency[$v['id']]['tc'] = $v['tc'];    //划转手续费
                }
            }
        }

        return ['waller'=>$currency,'total'=>$total,'total_freeze'=>$total_freeze];
    }

    /**
     * 更新用户余额
     * @param $mid 用户ID
     * @param $name 币种名称
     * @param $money 金额
     * @param int $type 1:非冻结余额 2:冻结余额
     * @return false|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function up_balance($mid,$name,$money,$type = 1){
        $currency = CurrencyModel::getDetailsName("name",$name);
        if(empty($currency)) return false;

        $currency = $currency->toArray();

        //不允许用户使用 或 币种已关闭
        if($currency['business'] == 2 || $currency['status'] == 2) return false;
        $member_currency = self::where('mid',$mid)->where('cid',$currency['id'])->find()->toArray();

        if($type == 1){ //非冻结
            $update['money'] = bcadd($member_currency['money'],$money,10);
        }else{  //冻结
            $update['freeze_money'] = bcadd($member_currency['freeze_money'],$money,10);
        }
        //更新金额
        self::where(['mid'=>$mid,'cid'=>$currency['id']])->update($update);

        return true;
    }

}